package id.co.shuba.spamobile.list;

import id.co.shuba.spamobile.SimpleCursorLoader;
import id.co.shuba.spamobile.data.ISharedDB;
import id.co.shuba.spamobile.data.ProspectDB;
import id.co.shuba.spamobile.widget.PipelineCursorAdapter;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

public class PipelineListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {
	
	public interface OnPipelineSelectedListener {
        public void onPipelineSelected(String id);
    }
			
	private OnPipelineSelectedListener _listener;
	
	private ProspectDB _db;
	private PipelineCursorAdapter _adapter;
	
	private String _aoCode;
	private String _filter;
	private String _sort;
	private int _type;
	
	public void setOnPipelineSelectedListener(OnPipelineSelectedListener listener) {
		_listener = listener;		
	}

	public static final class PipelineCursorLoader extends SimpleCursorLoader {
		private final String _aoCode;
		private final String _filter;
		private final int _type; 
		private final String _sort; 
		
		private final ProspectDB _db;

		public PipelineCursorLoader(Context context, ProspectDB db, String aoCode, String filter, int type, String sort) {
			super(context);
			_db = db;
			_aoCode = aoCode;
			_filter = filter;
			_type = type;
			_sort = sort;
		}			

		@Override
		public Cursor loadInBackground() {
			Cursor cursor = null;
								
			String filter = _filter == null? "" : _filter;			
			
			if (_type == 0)
				cursor = _db.getList(_aoCode, filter, _sort);			
			else
				cursor = _db.getList(_aoCode, filter, _type == 2, _sort);
			
			return cursor;
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (_listener == null)
			return;
						
		Cursor c = _adapter.getCursor();
		c.moveToPosition(position);
		_listener.onPipelineSelected(c.getString(c.getColumnIndex("PROSPECT_ID")));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		_adapter = new PipelineCursorAdapter(getActivity(), null, true);             	    	        	
		setListAdapter(_adapter);		
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		_db = new ProspectDB(((ISharedDB)getActivity()).getDB());
        getLoaderManager().initLoader(0, null, this);
	}
	public void setAoCode(String aoCode) {
		_aoCode = aoCode;
	}

	public void setFilter(String filter) {
		_filter = filter;				
	}	
	
	public void setType(int type) {
		_type = type;		
	}
	
	public void setSort(String sort) {
		_sort = sort;		
	}
	
	public void reload() {		
		Bundle bundle = new Bundle();
		bundle.putString("aoCode", _aoCode);
		bundle.putString("filter", _filter);
		bundle.putInt("type", _type);
		bundle.putString("sort", _sort);		
		
	    getLoaderManager().restartLoader(0, bundle, this);
	}
	
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String aoCode = null;
		String filter = null;
		int type = 0;
		String sort = null;
		
		if (args != null) {
			aoCode = args.getString("aoCode");
			filter = args.getString("filter");
			type = args.getInt("type");
			sort = args.getString("sort");
		}
		
		return new PipelineCursorLoader(getActivity(), _db, aoCode, filter, type, sort);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		_adapter.swapCursor(cursor);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		_adapter.swapCursor(null);
	}
}
